package com.chief.hadoop.fs;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.viewfs.ViewFileSystem;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class ViewfsAPI {

    String VIEWFS_PATH = "viewfs://mycluster/";

    @Test
    public void testStatus() throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI(VIEWFS_PATH), conf);
        FileStatus fileStatus = fileSystem.getFileStatus(new Path("/data1/api/java.txt"));
        System.out.println(fileStatus.toString());
        System.out.println(fileStatus.getPath().getName());
        System.out.println(fileStatus.isFile());
    }

    @Test
    public void testLs() throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI(VIEWFS_PATH), conf);
        RemoteIterator<LocatedFileStatus> iterator = fileSystem.listLocatedStatus(new Path("/"));
        while (iterator.hasNext()) {
            System.out.println(iterator.next().getPath().getName());
        }
    }

    @Test
    public void testRm() throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI(VIEWFS_PATH), conf);
        fileSystem.deleteOnExit(new Path("/data2/a.txt"));
    }

    @Test
    public void testMv() throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI(VIEWFS_PATH), conf);
        fileSystem.rename(new Path("/data2/a2.txt"), new Path("/data2/a.txt"));
        fileSystem.rename(new Path("/data2/out"), new Path("/data2/out111"));
    }

    @Test
    public void testDownLoad() throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI(VIEWFS_PATH), conf);
        fileSystem.copyToLocalFile(false, new Path("/data2/out/part-r-00000"), new Path("E:\\"), true);
        fileSystem.copyToLocalFile(false, new Path("/data2/out"), new Path("E:\\"), true);
    }

    @Test
    public void testUpload() throws IOException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = ViewFileSystem.newInstance(new URI(VIEWFS_PATH), conf);
        fileSystem.copyFromLocalFile(false, true, new Path("E:\\data2.txt"), new Path("/data2/"));
    }


}
